//
//  guangfangfabuViewController.m
//  xinhua_sx
//
//  Created by love on 14-8-8.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "guangfangfabuViewController.h"
#import "XHzhangShangViewController.h"
#import "XHTableViewHeaderView.h"
#import "XHZhangShangViewModel.h"
#import <MBProgressHUD.h>
#import "MainEnterViewController.h"
#import "dangeViewController.h"

@interface guangfangfabuViewController ()<UITableViewDelegate>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) XHZhangShangViewModel *viewModel;
@property (nonatomic ,strong) MBProgressHUD *HUD;
@end

@implementation guangfangfabuViewController
@synthesize titleLable = _titleLable;
@synthesize navBar= _navBar;
@synthesize neewId;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"ZhangShangView" owner:self options:nil];
    UIView *view = [array objectAtIndex:0];
    CGRect rect = self.view.frame;
    CGFloat oY = kIOS_VERSION<7.0?44:64;
    self.titleLable.text = @"官方发布";
    [view setFrame:CGRectMake(0, oY, rect.size.width, rect.size.height-oY)];
    [self.view addSubview:view];
    
    CGFloat aY = kIOS_VERSION<7.0?0:20;
    _navBar = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0+aY)];
    _navBar.backgroundColor = [UIColor colorWithRed:2.0/255 green:102.0/255 blue:172.0/255 alpha:1.0];
    _navBar.userInteractionEnabled = YES;
    
    _titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, aY, self.view.frame.size.width, 44)];
    _titleLable.backgroundColor = [UIColor clearColor];
    _titleLable.text = @"官方发布"  ;
    _titleLable.textColor = [UIColor whiteColor];
    _titleLable.font = [UIFont systemFontOfSize:20];
    _titleLable.textAlignment = NSTextAlignmentCenter;
    [_navBar addSubview:_titleLable];
    [self.view addSubview:_navBar];
    UIButton* _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, aY, 40,40.0)];
    [_leftButton setImage:[UIImage imageNamed:@"icon_back@2x.png"] forState:UIControlStateNormal];
    _leftButton.imageEdgeInsets = UIEdgeInsetsMake(10, 13, 5, 5);
    [_leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_leftButton];
    @weakify(self);
    //self.rightButton.hidden = YES;
    self.viewModel = [[XHZhangShangViewModel alloc] init:neewId huiming:@"官方"];
    NSLog(@"id33 = %@",neewId);
    NSUserDefaults* defa = [NSUserDefaults standardUserDefaults];
    
    
    
    self.viewModel.titleArray = [defa objectForKey:@"官方栏目"];
    self.viewModel.tempdataArray = [defa objectForKey:@"官方新闻"];

   // self.viewModel.defId = @"官方";
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(10, oY+5, 300, self.view.frame.size.height-oY-5);
    self.tableView.dataSource = self.viewModel;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.bounces = NO;
    
    
    [RACObserve(self.viewModel, hasLoad) subscribeNext:^(id x) {
        if ([x intValue]) {
            @strongify(self);
            [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:YES];
        }
    }];
    self.HUD = [[MBProgressHUD alloc] initWithView:view];
    [self.view addSubview:self.HUD];
    
    self.HUD.labelText = @"正在加载";
    self.HUD.dimBackground = NO;
    [self.HUD show:YES];
    if ([self.viewModel.titleArray count] > 0)
    {
        [self refresh];
    }

}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)refresh
{
    [self.tableView reloadData];
    [self.HUD hide:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* string = [[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    NSLog(@"string = %@",string);
    
    
    if ([string isEqualToString:@"县区"])
    {
    
    }
    else if ([string isEqualToString: @"地市"])
    {
        
    }
    else if ([string isEqualToString: @"山西省"])
    {
        
    }
    else if ([string isEqualToString:@"全国"])
    {
        
    }
    dangeViewController* viewContr = [[dangeViewController alloc] init];
    viewContr.newdId = [[[self.viewModel.titleArray objectAtIndex:indexPath.row] objectForKey:@"id"] stringValue];
    NSLog(@"id = %@",viewContr.newdId);
    viewContr.titleString = string;
    [self.navigationController pushViewController:viewContr animated:YES];
    butoncl* bl = [[butoncl alloc] init];
    //NSString* str = [viewControoler.yinitData[@"id"] stringValue];
    [bl postString:@"001" fff:viewContr.newdId];


}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
